package com.eeepay.eeepay_v2.activity;

import android.os.SystemClock;
import android.text.TextUtils;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.WithdrawalRecordAdapter;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshBase;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshListView;
import com.eeepay.v2_library.ui.ABBaseActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.eeepay.api.grpc.nano.WithdrawCashApiServiceGrpc;
import cn.eeepay.api.grpc.nano.WithdrawCashProto;
import io.grpc.ManagedChannel;


/**
 * 描述：账号记录
 * 作者：zhuangzeqin
 * 时间: 2017/5/3-11:43
 * 邮箱：zzq@eeepay.cn
 */
public class WithdrawalRecordActivity extends ABBaseActivity {
    private static final int REQUEST_RECORD_TASKID = 1000;//请求是任务id
    private PullToRefreshListView lv_record;//提现记录列表
    private WithdrawalRecordAdapter recordAdapter;//适配器
    private List<WithdrawCashProto.AccountTranInfo> withdrawalRecordInfos;//数据集合
    private int currPage = 1;//当前页
    private int pageSize = 10;//每页显示10条
    private int total = 0;//总的记录数;后台返回

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal_record;
    }

    @Override
    protected void initWidget() {
        lv_record = getViewById(R.id.lv_record);
        recordAdapter = new WithdrawalRecordAdapter(mContext);
        withdrawalRecordInfos = new ArrayList<>();
        recordAdapter.setList(withdrawalRecordInfos);
        lv_record.setAdapter(recordAdapter);
        sendHttpRequest(REQUEST_RECORD_TASKID);
    }

    @Override
    protected void eventOnClick() {
        lv_record.setMode(PullToRefreshBase.Mode.BOTH);
        lv_record.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            /** 往下拉取 获取最新数据 **/
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currPage = 1;
                sendHttpRequest(REQUEST_RECORD_TASKID);
            }

            /** 往上加载 ，加载更多数据**/
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (total > currPage * pageSize) {//如果还小于总的大小；接着请求下10条记录
                    currPage++;
                    sendHttpRequest(REQUEST_RECORD_TASKID);
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            SystemClock.sleep(1000);
                            showToast("已经是最后一页了");
                            if (lv_record.isRefreshing()) {
                                lv_record.onRefreshComplete();
                            }
                        }
                    });
                }
            }
        });
    }

    /**
     * 开始网络请求
     *
     * @param taskId
     */
    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                WithdrawCashApiServiceGrpc.WithdrawCashApiServiceBlockingStub sub = WithdrawCashApiServiceGrpc.newBlockingStub(channel);
                if (REQUEST_RECORD_TASKID==taskId)
                {
                    WithdrawCashProto.AccountTranInfoRequest tranInfoRequest = new WithdrawCashProto.AccountTranInfoRequest();
                    tranInfoRequest.agentNo= UserInfo.getUserInfo2SP().getAgentNo();//代理商编号
                    tranInfoRequest.pageSize = pageSize;
                    tranInfoRequest.pageNo = currPage;
                    return sub.getAccountTranInfo(tranInfoRequest);
                }
                return null;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if (lv_record.isRefreshing()) {
                    lv_record.onRefreshComplete();
                }
                if (REQUEST_RECORD_TASKID==taskId)
                {
                    WithdrawCashProto.AccountTranInfoResponse infoResponse = (WithdrawCashProto.AccountTranInfoResponse)result;
                    if (infoResponse==null) return;
                    if (!infoResponse.baseResponse.status)
                    {
                        showToast(infoResponse.baseResponse.msg);
                        return;
                    }
                    if(!TextUtils.isEmpty(infoResponse.count)
                            && TextUtils.isDigitsOnly(infoResponse.count)){
                        total = Integer.parseInt(infoResponse.count);
                    }
                    if (total==0)
                    {
                        showToast("暂时没有提现记录");
                        return;
                    }
                    WithdrawCashProto.AccountTranInfo[] accountTranInfos = infoResponse.accountTranInfo;
                    if (currPage == 1) {
                        recordAdapter.setList(Arrays.asList(accountTranInfos));
                    } else {
                        recordAdapter.addAll(Arrays.asList(accountTranInfos));
                    }
                }
            }
        });
    }
}
